package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import javax.xml.transform.Templates;

public class RegisterActivity extends AppCompatActivity {
    private TextView tv_main_title;//标题
    private TextView tv_back;//返回按钮
    private Button btn_register;//注册按钮
    private EditText et_user_name,et_psw,et_psw_again;//输入用户名,密码,再次输入密码控件
    private String userName,psw,pswAgain;//获取用户名,密码,再次输入密码控件的值
    private RelativeLayout rl_title_bar;//标题布局
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//改变屏幕方向
        init();
    }

    private void init() {
        tv_main_title=(TextView)findViewById(R.id.title_text);//标题
        tv_main_title.setText("注      册");
        tv_back=(TextView)findViewById(R.id.title_back);//返回按钮
        rl_title_bar=(RelativeLayout)findViewById(R.id.include);//头导航栏
        btn_register=(Button) findViewById(R.id.button);
        et_user_name=(EditText)findViewById(R.id.edit_user);
        et_psw=(EditText)findViewById(R.id.edit_pw);
        et_psw_again=(EditText)findViewById(R.id.edit_pw_ag);
        tv_back.setOnClickListener(new View.OnClickListener() {//返回按钮点击方法
            @Override
            public void onClick(View view) {
                RegisterActivity.this.finish();//杀掉此页面
            }
        });
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getEditString();
                String temp="请输入";
                if(TextUtils.isEmpty(userName)){
                    temp=temp+" 用户名";
                }
                if (TextUtils.isEmpty(psw)){
                    temp=temp+" 密码";
                }
                if (TextUtils.isEmpty(pswAgain)){
                    temp=temp+"再次输入密码";
                }
                if (!psw.equals(pswAgain)&&pswAgain!=null&&psw!=null){
                    temp="两次输入的密码不一样";
                }
                if (isExistUserName(userName)){
                    temp="输入的账号已存在";
                }
                if (!temp.equals("请输入")){
                    Toast.makeText(RegisterActivity.this,temp,Toast.LENGTH_LONG).show();
                    return;
                }
                else{
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                    /*此处是保存方法*/
                    Intent data=new Intent();
                    data.putExtra("userName",userName);
                    setResult(RESULT_OK,data);
                    RegisterActivity.this.finish();
                }
            }
        });
    }

    private void getEditString(){
        userName=et_user_name.getText().toString().trim();
        psw=et_psw.getText().toString().trim();
        pswAgain=et_psw_again.getText().toString().trim();
    }
/*SharedPreferences是键值对储存方法,储存在xml文件中*/
    private boolean isExistUserName(String userName){//判断用户是否存在
        boolean has_user=false;
        SharedPreferences sp=getSharedPreferences("loginInfo",MODE_PRIVATE);//获取文件
        String spPsw=sp.getString(userName,"");//获取文件用户名
        if (!TextUtils.isEmpty(spPsw)){
            has_user=true;
        }
        return  has_user;
    }
    private void saveUser(){
        String pwEncry=/*加密密码方法*/;
        SharedPreferences sp=getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor ed=sp.edit();/*编辑器*/
        ed.putString(userName,pwEncry);
        ed.commit();/*提交对文件的修改*/
    }
}
